package org.example.test1;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class 统计每月人数条件持续时间 {
    public static void main(String[] args) {
        String start = "2024-01-01";
        String end = "2024-05-01";
        int code = 1;

        Group.Unit unit = Group.Unit.find(code);

        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
        LocalDate startDate = LocalDate.parse(start, formatter);
        LocalDate endDate = LocalDate.parse(end, formatter);

        Map<Group, Counter> map = Group.generator(unit, startDate, endDate);
        String[] str={"2024-01-01","2024-05-01","2024-01-23"};
        //获取患者信息里面的最大和最小
//        foreach遍历 {
//            LocalDate startTime = 患者maxdate;
//            LocalDate endTime = 患者minxdate;
//            while (!startTime.isAfter(endTime)) {
                List<LocalDate> records = Arrays.stream(str).map(e -> LocalDate.parse(e, formatter)).collect(Collectors.toList());
                records.forEach(e -> {//遍历 records 集合中的每个元素 e
                    for (Map.Entry<Group, Counter> entry : map.entrySet()) {//entrySet() 返回的集合支持直接操作键值对
                        //entrySet() 可以方便地遍历 Map 中的所有键值对，同时访问每个键（Group）和对应的值（Counter）
                        Group group = entry.getKey();
                        if (group.compareTo(e) == 0) {
                            Counter counter = entry.getValue();
                            counter.increment();
                        }
                    }
                });
//            }
//        }

        String[] str2={"2024-04-01","2024-05-01","2024-01-23"};
        List<LocalDate> patientRecords= Arrays.stream(str2).map(e->LocalDate.parse(e,formatter)).collect(Collectors.toList());
        patientRecords.forEach(e->{
            for(Map.Entry<Group, Counter> entry:map.entrySet()){
                Group group = entry.getKey();
                if(group.compareTo(e)==0){
                    Counter counter = entry.getValue();
                    counter.patientIncrement();
                }
            }
        });

        System.out.println(map);
    }
}
